/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_contact_ipport.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/12 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "../../comm/frcmd_parse_detail.h"
#include "../../comm/client.pb-c.h"
#include "../../comm/frcmd_parse.h"
#include "../../comm/apm.pb-c.h"
#include "../../comm/frcmd_client.h"
#include "../../comm/frcomm.h"
#include "../../comm/frmem.h"
#include "../../comm/frsutl.h"
#include "../../comm/apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

void case1(void)
{
    char *p_ip = NULL;
    char *p_port = NULL;
    char *p_str = NULL;

    p_ip = malloc(strlen("192.168.1.1") + 1);
    snprintf(p_ip,strlen("192.168.1.1") + 1,"%s","192.168.1.1");

    p_port = malloc(strlen("80") + 1);
    snprintf(p_port,strlen("80") + 1,"%s","80");

    p_str = contact_ipport(p_ip,p_port);

    CU_ASSERT_STRING_EQUAL(p_str,"192.168.1.1|80");

    free(p_str);

    return;

}

void case2(void)
{
    char *p_ip = NULL;
    char *p_port = NULL;
    char *p_str = NULL;

    p_ip = malloc(strlen("192.168.1.1-192.168.1.10") + 1);
    snprintf(p_ip,strlen("192.168.1.1-192.168.1.10") + 1,"%s","192.168.1.1-192.168.1.10");

    p_port = malloc(strlen("80-100") + 1);
    snprintf(p_port,strlen("80-100") + 1,"%s","80-100");

    p_str = contact_ipport(p_ip,p_port);

    CU_ASSERT_STRING_EQUAL(p_str,"192.168.1.1-192.168.1.10|80-100");

    free(p_str);

    return;

}

void case3(void)
{
    char *p_ip = NULL;
    char *p_port = NULL;
    char *p_str = NULL;

    p_ip = malloc(strlen("192.168.1.1") + 1);
    snprintf(p_ip,strlen("192.168.1.1") + 1,"%s","192.168.1.1");

    p_str = contact_ipport(p_ip,p_port);

    CU_ASSERT_STRING_EQUAL(p_str,"192.168.1.1");

    free(p_str);

    return;

}

void case4(void)
{
    char *p_ip = NULL;
    char *p_port = NULL;
    char *p_str = NULL;

    p_port = malloc(strlen("80-100") + 1);
    snprintf(p_port,strlen("80-100") + 1,"%s","80-100");

    p_str = contact_ipport(p_ip,p_port);

    CU_ASSERT_STRING_EQUAL(p_str,"|80-100");

    free(p_str);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t_contact_ipport 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

   	CU_add_test(pSuite, "case2", case2);

   	CU_add_test(pSuite, "case3", case3);

   	CU_add_test(pSuite, "case4", case4);

    return 0;

}

int main(int argc, char **argv)
{
    frmem_init();
    run_cunit(add_suite);
    frshow_leak();
    frmem_destroy();
    return 0;
}